package liy.demo.wikiproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import liy.demo.wikiproject.domain.User;
import liy.demo.wikiproject.exception.BookException;
import liy.demo.wikiproject.exception.BookExceptionCode;
import liy.demo.wikiproject.mapper.UserMapper;
import liy.demo.wikiproject.req.UserQueryReq;
import liy.demo.wikiproject.req.UserSaveReq;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.UserResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.util.CopyUtil;
import liy.demo.wikiproject.util.LoginUserContext;
import liy.demo.wikiproject.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    SnowFlake snowFlake;


    @Autowired
    RedisTemplate redisTemplate;
    public UserResp validate(UserQueryReq req){
        //
        User user = userMapper.selectByLoginName(req.getLoginName());
        if(user==null) {
            //登录失败
            throw  new BookException(BookExceptionCode.USER_LOGIN_WRONG_PASSWORD);
        }else{
            if(user.getPassword().equals(req.getPassword())) {
                //登录成功
                UserResp userResp = CopyUtil.copy(user,UserResp.class);
                LoginUserContext.setUser(userResp);
                String token = generateToken(user.getId());
                userResp.setToken(token);
                redisTemplate.opsForValue().set(token,"1");
                return userResp;
            }else{
                throw  new BookException(BookExceptionCode.USER_LOGIN_WRONG_PASSWORD);

            }
        }
    }


    public UserResp info(String token) {
        Long Id = getUserIdFromToken(token);

        User user = userMapper.selectByPrimaryKey(Id);

        UserResp resp = CopyUtil.copy(user, UserResp.class);
        return resp;
    }
    @CachePut(value="tokens",key = "#user.id")
    public String generateToken(Long userId) {
        // 这里是一个简单的生成token的示例,你可以根据自己的需求进行修改
        String token = userId + "_" + System.currentTimeMillis();
        // 将token保存到Redis中,设置过期时间
        redisTemplate.opsForValue().set("token_" + userId, token, 30, TimeUnit.MINUTES);
        return token;
    }

    private Long getUserIdFromToken(String token){
            String[]parts = token.split("_");
            if(parts.length==2) {
                Long userId = Long.parseLong(parts[0]);

                String s =(String) redisTemplate.opsForValue().get("token_" + userId);

                if(token.equals(s)) {
                    return  userId;
                }
            }
            return -1L;
    }

    public void delete(String token) {
        redisTemplate.delete(token);

    }
}
